﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ArtOfTest.WebAii.Controls.HtmlControls;
using ArtOfTest.Common;
using SM2Utilities;

namespace SM2Profile
{
    public class ProfileActions
    {
        public static void loadProfile(string strSelectedProfile)
        {
            HtmlControl controlOpen = ProfileActions.getControlOpenByProfileName(strSelectedProfile);
            controlOpen.Click();
            Utilities.GlobalObject.MANAGER.ActiveBrowser.WaitUntilReady();
            System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_TIMEOUT_MILISECOND);
        }

        public static void actionConfirmOnPopup()
        {
            //Refresh DomTree
            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
            HtmlDiv cphPageContent_pnlMPAddEditProfile = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("cphPageContent_pnlMPAddEditProfile");

            if (Profile.strConfirm.Equals("no"))
            {
                HtmlInputSubmit cphPageContent_btnCancel = cphPageContent_pnlMPAddEditProfile.Find.ById<HtmlInputSubmit>("cphPageContent_btnCancel");
                SM2Input.Tradition.ClickButton(cphPageContent_btnCancel);
            }
            else
            {
                HtmlInputSubmit cphPageContent_saveProfile = cphPageContent_pnlMPAddEditProfile.Find.ById<HtmlInputSubmit>("cphPageContent_saveProfile");
                SM2Input.Tradition.ClickButton(cphPageContent_saveProfile);
                //Wait
                Utilities.GlobalObject.MANAGER.ActiveBrowser.WaitUntilReady();
                System.Threading.Thread.Sleep(Utilities.GlobalConfig.INT_WAIT_TIMEOUT_MILISECOND);
            }
        }

        public static void actionInputTextOnPopup(string strProfileName)
        {
            //Refresh DomTree
            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();
            HtmlDiv cphPageContent_pnlMPAddEditProfile = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlDiv>("cphPageContent_pnlMPAddEditProfile");
            HtmlInputText cphPageContent_txtCreateProfile = cphPageContent_pnlMPAddEditProfile.Find.ById<HtmlInputText>("cphPageContent_txtCreateProfile");
            SM2Input.Tradition.TypeTextIntoTextField(cphPageContent_txtCreateProfile, strProfileName);
        }

        public static Boolean isProfileNameExisted(string strSelectedProfile)
        {
            //Refresh DomTree
            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();

            HtmlTable cphPageContent_gvProfiles = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlTable>("cphPageContent_gvProfiles");
            int intCountRows = cphPageContent_gvProfiles.Rows.Count - 1;
            for (int i = 1; i < intCountRows; i++)
            {
                HtmlTableRow tblrow = cphPageContent_gvProfiles.AllRows[i];
                int int_tblrow = tblrow.Cells.Count;
                HtmlTableCell tblCellName = tblrow.Cells[0];
                if (int_tblrow == 5)
                {
                    tblCellName = tblrow.Cells[0];
                }
                else if (int_tblrow == 6)
                {
                    tblCellName = tblrow.Cells[1];
                }
                IList<HtmlSpan> iListSpan = tblCellName.Find.AllByAttributes<HtmlSpan>();

                HtmlSpan spanName = iListSpan[0];
                string strCellName = spanName.TextContent.Trim();
                if (strCellName.Equals(strSelectedProfile))
                {
                    return true;
                }
            }
            return false;
        }

        public static HtmlControl getControlOpenByProfileName(string strSelectedProfile)
        {
            //Refresh DomTree
            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();

            //Get reference to Table of Profiles
            HtmlTable cphPageContent_gvProfiles = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlTable>("cphPageContent_gvProfiles");
            int intCountRows = cphPageContent_gvProfiles.Rows.Count - 1;
            for (int i = 1; i < intCountRows; i++)
            {
                HtmlTableRow tblrow = cphPageContent_gvProfiles.AllRows[i];
                int int_tblrow = tblrow.Cells.Count;
                HtmlTableCell tblCellName = tblrow.Cells[0];
                if (int_tblrow == 5)
                {
                    tblCellName = tblrow.Cells[0];
                }
                else if (int_tblrow == 6)
                {
                    tblCellName = tblrow.Cells[1];
                }
                IList<HtmlSpan> iListSpan = tblCellName.Find.AllByAttributes<HtmlSpan>();

                HtmlSpan spanName = iListSpan[0];
                string strCellName = spanName.TextContent;
                if (strCellName.Equals(strSelectedProfile))
                {
                    HtmlTableCell tblCell = tblrow.Cells[1];
                    if (int_tblrow == 5)
                    {
                        tblCell = tblrow.Cells[1];
                    }
                    else if (int_tblrow == 6)
                    {
                        tblCell = tblrow.Cells[2];
                    }
                    IList<HtmlControl> iListControls = tblCell.Find.AllByAttributes<HtmlControl>();
                    HtmlControl control = iListControls[1];
                    return control;
                }
            }
            return null;
        }

        public static HtmlControl getControlRenameByProfileName(string strSelectedProfile)
        {
            //Refresh DomTree
            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();

            //Get reference to Table of Profiles
            HtmlTable cphPageContent_gvProfiles = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlTable>("cphPageContent_gvProfiles");
            int intCountRows = cphPageContent_gvProfiles.Rows.Count - 1;
            for (int i = 1; i < intCountRows; i++)
            {
                HtmlTableRow tblrow = cphPageContent_gvProfiles.AllRows[i];
                int int_tblrow = tblrow.Cells.Count;
                HtmlTableCell tblCellName = tblrow.Cells[0];
                if (int_tblrow == 5)
                {
                    tblCellName = tblrow.Cells[0];
                }
                else if (int_tblrow == 6)
                {
                    tblCellName = tblrow.Cells[1];
                }
                IList<HtmlSpan> iListSpan = tblCellName.Find.AllByAttributes<HtmlSpan>();

                HtmlSpan spanName = iListSpan[0];
                string strCellName = spanName.TextContent;
                if (strCellName.Equals(strSelectedProfile))
                {
                    HtmlTableCell tblCell = tblrow.Cells[2];
                    if (int_tblrow == 5)
                    {
                        tblCell = tblrow.Cells[2];
                    }
                    else if (int_tblrow == 6)
                    {
                        tblCell = tblrow.Cells[3];
                    }
                    IList<HtmlControl> iListControls = tblCell.Find.AllByAttributes<HtmlControl>();
                    HtmlControl control = iListControls[1];
                    return control;
                }
            }
            return null;
        }

        public static HtmlControl getControlDeleteByProfileName(string strSelectedProfile)
        {
            //Refresh DomTree
            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();

            HtmlTable cphPageContent_gvProfiles = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlTable>("cphPageContent_gvProfiles");
            int intCountRows = cphPageContent_gvProfiles.Rows.Count - 1;
            for (int i = 1; i < intCountRows; i++)
            {
                HtmlTableRow tblrow = cphPageContent_gvProfiles.AllRows[i];
                int int_tblrow = tblrow.Cells.Count;
                HtmlTableCell tblCellName = tblrow.Cells[0];
                if (int_tblrow == 5)
                {
                    tblCellName = tblrow.Cells[0];
                }
                else if (int_tblrow == 6)
                {
                    tblCellName = tblrow.Cells[1];
                }
                IList<HtmlSpan> iListSpan = tblCellName.Find.AllByAttributes<HtmlSpan>();

                HtmlSpan spanName = iListSpan[0];
                string strCellName = spanName.TextContent;
                if (strCellName.Equals(strSelectedProfile))
                {
                    HtmlTableCell tblCell = tblrow.Cells[3];
                    if (int_tblrow == 5)
                    {
                        tblCell = tblrow.Cells[3];
                    }
                    else if (int_tblrow == 6)
                    {
                        tblCell = tblrow.Cells[4];
                    }
                    IList<HtmlControl> iListControls = tblCell.Find.AllByAttributes<HtmlControl>();
                    HtmlControl control = iListControls[1];
                    return control;
                }
            }
            return null;
        }

        public static int getTotalCurrentResults()
        {
            int intCurrent = 0;

            //Refresh DomTree
            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();

            HtmlTable cphPageContent_gvProfiles = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlTable>("cphPageContent_gvProfiles");
            int intCountRows = cphPageContent_gvProfiles.Rows.Count - 1;
            HtmlTableRow tblrow = cphPageContent_gvProfiles.AllRows[intCountRows];
            
            int int_tblrow = tblrow.Cells.Count;
            HtmlTableCell tblCellNumber = tblrow.Cells[4];
            if (int_tblrow == 5)
            {
                tblCellNumber = tblrow.Cells[4];
            }
            else if (int_tblrow == 6)
            {
                tblCellNumber = tblrow.Cells[5];
            }

            IList<HtmlSpan> iListControls = tblCellNumber.Find.AllByAttributes<HtmlSpan>();
            HtmlSpan span = iListControls[0];
            string strTotal = span.TextContent;
            string strCurrentResults = strTotal.Split('/')[0].Trim();
            intCurrent = int.Parse(strCurrentResults);

            return intCurrent;
        }

        public static int getNumberResultByProfileName(string strSelectedProfile)
        {
            int intCurrent = 0;
            //Refresh DomTree
            Utilities.GlobalObject.MANAGER.ActiveBrowser.RefreshDomTree();

            HtmlTable cphPageContent_gvProfiles = Utilities.GlobalObject.MANAGER.ActiveBrowser.Find.ById<HtmlTable>("cphPageContent_gvProfiles");
            int intCountRows = cphPageContent_gvProfiles.Rows.Count - 1;
            for (int i = 1; i < intCountRows; i++)
            {
                HtmlTableRow tblrow = cphPageContent_gvProfiles.AllRows[i];
                
                int int_tblrow = tblrow.Cells.Count;
                HtmlTableCell tblCellName = tblrow.Cells[0];
                if (int_tblrow == 5)
                {
                    tblCellName = tblrow.Cells[0];
                }
                else if (int_tblrow == 6)
                {
                    tblCellName = tblrow.Cells[1];
                }

                IList<HtmlSpan> iListSpan = tblCellName.Find.AllByAttributes<HtmlSpan>();
                HtmlSpan spanName = iListSpan[0];
                string strCellName = spanName.TextContent;
                if (strCellName.Equals(strSelectedProfile))
                {
                    HtmlTableCell tblCellNumber = tblrow.Cells[4];
                    if (int_tblrow == 5)
                    {
                        tblCellNumber = tblrow.Cells[4];
                    }
                    else if (int_tblrow == 6)
                    {
                        tblCellNumber = tblrow.Cells[5];
                    }

                    IList<HtmlSpan> iListControls = tblCellNumber.Find.AllByAttributes<HtmlSpan>();
                    HtmlSpan span = iListControls[0];
                    string strTotal = span.TextContent;
                    intCurrent = int.Parse(strTotal);
                    break;
                }
            }
            return intCurrent;
        }
    }
}